package js.web.center.utils;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class JsWebDesUtils {
	public static byte[] encrypt(byte[] data, byte[] key) {
		// 不足16字节，补齐内容为差值
		int len = 16 - data.length % 16;
		for (int i = 0; i < len; i++) {
			byte[] bytes = { (byte) len };
			data = ArrayUtils.concat(data, bytes);
		}
		try {
			SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
			Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
			cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
			return cipher.doFinal(data);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new byte[] {};
	}

	public static int byteToInt(byte b) {
		return (b) & 0xff;
	}

	public static byte[] decrypt(byte[] data, byte[] key) {
		data = ArrayUtils.noPadding(data, -1);
		try {
			SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
			Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
			cipher.init(Cipher.DECRYPT_MODE, skeySpec);
			byte[] decryptData = cipher.doFinal(data);
			int len = 2 + byteToInt(decryptData[4]) + 3;
			return ArrayUtils.noPadding(decryptData, len);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new byte[] {};
	}
}
